<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>新增邮箱</title>
</head>
<body>
<div id="adde">
    <p>
        请输入邮箱：<input type="email" @blur="verifyEmail" v-model="email.email_detail" placeholder="账号@qq.com"><span>{{msg}}</span>
    </p>
    <input type="button" value="增加" @click="add">
    <input type="button" value="返回" @click="returnPages">
</div>
<script src="assets/js/vue.js"></script>
<script src="assets/js/axios.min.js"></script>
<script>
    const addEmail = new Vue({
        el: "#adde",
        data: {
            msg: "",
            email: {
                email_detail: "",
            }
        },
        methods: {
            returnPages() {
                location.href = "invoice-info-manage.html";
            },
            regex(emailName) {
                let pattern = /^[a-zA-Z0-9_.-]+@[a-zA-Z0-9-]+(\.[a-zA-Z0-9-]+)*\.(com|cn|net)$/;
                if (pattern.test(emailName)) {
                    this.msg = "格式合法";
                    return true;
                } else {
                    this.msg = "格式不合法";
                    return false;
                }
            },
            async verifyEmail() {
                let formData = new FormData();
                let _this = this;
                let flag = false;
                let emailName = _this.email.email_detail;
                formData.append("emailName", emailName);
                let verify = this.regex(emailName);

                if (verify) {
                    await axios({
                        method: "post",
                        url: "../admin/checkEmailName.email",
                        data: formData
                    }).then(function (response) {
                        let data = response.data;
                        if (data.code == 440) {
                            _this.msg = data.msg
                            flag = false;
                        }
                        if (data.code == 441) {
                            if (emailName != "") {
                                _this.msg = data.msg
                                flag = true;
                            }
                        }
                    })
                }
                return flag;
            },
            add() {
                let _this = this;
                let result = this.verifyEmail();
                let formData = new FormData();
                formData.append("email", _this.email.email_detail);
                // console.log(result);
                result.then(result => {
                    if (result) {
                        if (_this.email.email_detail != "") {
                            axios({
                                method: "post",
                                url: "../admin/addEmail.email",
                                data: formData
                            }).then(function (response) {
                                let data = response.data;
                                console.log(_this.email.email_detail);
                                alert(data.msg)
                                if (data.code == 403) {
                                    location.href = "invoice-info-manage.html";
                                }
                            })
                        } else {
                            alert("请输入邮箱!");
                            location.reload();
                        }
                    } else {
                        alert("邮箱已经存在或者不合法,请重新输入");
                        location.reload();
                    }
                });
            }
        }
    });
</script>
</body>
</html>